package com.life.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.life.mapper.ArticleMapper;
import com.life.pojo.dto.ArticleQueryDTO;
import com.life.pojo.vo.ArticleVO;
import com.life.pojo.vo.TagVO;
import com.life.result.PageResult;
import com.life.result.Result;
import com.life.service.ArticleService;
import com.life.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 分页查询文章
     * @return
     */
    @GetMapping("/article")
    @ApiOperation("分页查询文章")
    public Result<PageResult> pageQuery(ArticleQueryDTO articleQueryDTO) {
        log.info("分页查询文章：{}", articleQueryDTO);
        if(articleQueryDTO.getPageNum() == null || articleQueryDTO.getPageSize() == null) {
            articleQueryDTO.setPageNum(1);
            articleQueryDTO.setPageSize(10);
        }
        PageHelper.startPage(articleQueryDTO.getPageNum(), articleQueryDTO.getPageSize());
        Page<ArticleVO> page = articleMapper.pageQueryByUser(articleQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return Result.success(pageResult);
    }

    /**
     * 根据id获取文章
     * @param id
     * @return
     */
    @GetMapping("/article/{id}")
    @ApiOperation("根据id获取文章")
    public Result<ArticleVO> getArticleById(@PathVariable Long id) {
        log.info("根据id获取文章: {}", id);
        ArticleVO article = articleService.getArticleById(id);
        return Result.success(article);
    }

    /**
     * 获取标签列表
     * @return
     */
    @GetMapping("/tag")
    @ApiOperation("获取标签列表")
    public Result<List<TagVO>> getTagList() {
        List<TagVO> tagList = tagService.getTagList();
        return Result.success(tagList);
    }
}
